package com.wuxuan.algorithm_homework.graph;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 图邻接表的实现(无向图的)
 */
public class AdjacencyListDemo {

    private int numVertices;
    private List<List<Integer>> adjList;

    public AdjacencyListDemo(int numVertices) {
        this.numVertices = numVertices;
        adjList = new ArrayList<>();
        for (int i = 0; i < numVertices; i++) {
            adjList.add(new LinkedList<>());
        }
    }

    private void addEdge(int src, int dest) {
        adjList.get(src).add(dest);
        adjList.get(dest).add(src);
    }

    private void printAdjList() {
        for (int i = 0; i < numVertices; i++) {
            System.out.println("Vertex " + i + " neighbors: ");
            List<Integer> list = adjList.get(i);
            for (int j = 0; j < list.size(); j++) {
                System.out.print(list.get(j) + " ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        AdjacencyListDemo adj = new AdjacencyListDemo(4);
        adj.addEdge(0, 1);
        adj.addEdge(0, 2);
        adj.addEdge(1, 3);
        adj.addEdge(2, 3);
        adj.printAdjList();

    }


}
